package logic;

import javafx.util.Pair;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.*;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: SmartBoy
 * @Date: 2021/07/26/20:38
 * @Description:
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MyFile {

    private String name;     // 目录/文件名
    private String type;     // 文件类型（目录为null）
    private int attr;        // 目录/文件属性 （为8时表示目录）
    private int initBlockId; // 起始盘块号
    private int size;        // 文件大小 (字节)
    private String path;     // 绝对路径名
    private MyFile parent;   // 父目录
    private List<MyFile> childList;  // 子目录/文件集合
    private boolean isDirectory;     // 是否文件夹
    private boolean isOpen;  //是否打开状态
    private Pair<Integer, Integer> coordinate; //在父目录中该文件信息的磁盘坐标


    //目录建立
    public MyFile(String name, int initBlockId, MyFile parent) {
        this(name, null, 8, initBlockId, 0, parent.path + "/" + name, parent, new CopyOnWriteArrayList<>(), true, false, null);
    }

    //文件创建
    public MyFile(String name, String type, int attr, int initBlockId, MyFile parent) {
        this(name, type, attr, initBlockId, 0, parent.path + "/" + name + "." + type, parent, null, false, false, null);
    }

    //对文件/目录按名称排序
    public List<MyFile> getChildList() {
        if (childList != null) {
            Collections.sort(childList, new Comparator<MyFile>() {
                @Override
                public int compare(MyFile o1, MyFile o2) {
                    if (o1.isDirectory() && !o2.isDirectory())
                        return -1;
                    if (!o1.isDirectory() && o2.isDirectory())
                        return 1;
                    return o1.getName().compareTo(o2.getName());
                }
            });
        }
        return childList;
    }

    @Override
    public String toString() {
        String name = this.name;
        if (!this.isDirectory) name += "." + this.type;
        return name;
//        return "MyFile(" +
//                "name='" + name + '\'' +
//                ", type='" + type + '\'' +
//                ", attr=" + attr +
//                ", initBlockId=" + initBlockId +
//                ", size=" + size +
//                ", path=" + path +
//                ", childList=" + childList +
//                ", isDirectory=" + isDirectory +
//                ", isOpen=" + isOpen +
//                ')';
    }

}
